package com.ruoyi.web.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.SoftwareVersion;
import com.ruoyi.web.mapper.SoftwareVersionMapper;
import com.ruoyi.web.service.ISoftwareVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.SoftwaresMapper;
import com.ruoyi.web.domain.Softwares;
import com.ruoyi.web.service.ISoftwaresService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 软件Service业务层处理
 *
 * @author song
 * @date 2024-08-26
 */
@Service
public class SoftwaresServiceImpl implements ISoftwaresService
{
    @Autowired
    private SoftwaresMapper softwaresMapper;
    @Autowired
    private SoftwareVersionMapper versionMapper;
    @Autowired
    private ISoftwareVersionService softwareVersionService;

    /**
     * 查询软件
     *
     * @param softwareId 软件主键
     * @return 软件
     */
    @Override
    public Softwares selectSoftwaresBySoftwareId(Long softwareId)
    {
        return softwaresMapper.selectSoftwaresBySoftwareId(softwareId);
    }

    /**
     * 查询软件列表
     *
     * @param softwares 软件
     * @return 软件
     */
    @Override
    public List<Softwares> selectSoftwaresList(Softwares softwares)
    {
        return softwaresMapper.selectSoftwaresList(softwares);
    }

    /**
     * 新增软件
     *
     * @param softwares 软件
     * @return 结果
     */
    @Override
    public int insertSoftwares(Softwares softwares)
    {
        softwares.setCreateTime(DateUtils.getNowDate());
        softwares.setUpdateTime(DateUtils.getNowDate());
        return softwaresMapper.insertSoftwares(softwares);
    }

    /**
     * 修改软件
     *
     * @param softwares 软件
     * @return 结果
     */
    @Override
    public int updateSoftwares(Softwares softwares)
    {
        softwares.setUpdateTime(DateUtils.getNowDate());
        return softwaresMapper.updateSoftwares(softwares);
    }

    /**
     * 批量删除软件
     *
     * @param softwareIds 需要删除的软件主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSoftwaresBySoftwareIds(Long[] softwareIds)
    {
        ArrayList<Long> versionIds = new ArrayList<>();
        SoftwareVersion softwareVersion = null;
        // 查询每个软件对应的版本ID并收集所有版本ID
        for (Long softwareId : softwareIds) {
            softwareVersion = new SoftwareVersion();
            softwareVersion.setSoftwareId(softwareId);
            List<SoftwareVersion> versionList = versionMapper.selectSoftwareVersionList(softwareVersion);

            // 收集所有版本ID
            for (SoftwareVersion version : versionList) {
                versionIds.add(version.getVersionId());
            }
        }

        // 批量删除所有收集到的版本
        if (!versionIds.isEmpty()) {
            // 调用deleteSoftwareVersionByVersionIds方法删除对应版本ID的链接列表
            softwareVersionService.deleteSoftwareVersionByVersionIds(versionIds.toArray(new Long[0]));
        }
        // 删除软件
        return softwaresMapper.deleteSoftwaresBySoftwareIds(softwareIds);


    }

    /**
     * 删除软件信息
     *
     * @param softwareId 软件主键
     * @return 结果
     */
    @Override
    public int deleteSoftwaresBySoftwareId(Long softwareId)
    {
        return softwaresMapper.deleteSoftwaresBySoftwareId(softwareId);
    }
}
